﻿using CommunityToolkit.Mvvm.ComponentModel;
using KxApp.Utils;
using LiveCharts;
using LiveCharts.Wpf;
using System.Collections.ObjectModel;
using System.Windows.Media;

namespace KxApp.ViewModels
{
    [ExposedService(Lifetime.Singleton)]
    public partial class ChartViewModel : ObservableObject
    {
        [ObservableProperty]
        private string txt = "Test";

        public ChartViewModel()
        {
            #region 饼状图Cotr
            PieValues = new SeriesCollection();
            InitializePieData();

            #endregion

            #region 柱状图2Ctor
            BarChartSeries = new SeriesCollection();
            InitializeBarData();
            #endregion

            #region 折线图Ctor
            InitializeLineData();
            #endregion

            ////测试
            Task.Run(async () =>
            {
                while (true)
                {
                    await Task.Delay(1000);

                    //await Application.Current.Dispatcher.InvokeAsync(new Action(() =>
                    //{

                    //}));

                    var random = new Random();
                    A = random.Next(1, 100);
                    B = random.Next(1, 100);
                    C = random.Next(1, 100);
                }
            });
        }

        private double a = 2;
        public double A
        {
            get => a;
            set
            {
                if (SetProperty(ref a, value))
                {
                    UpdatePieData();
                    UpdateBarData();
                    UpdateLineData();
                }
            }
        }

        private double b = 3;
        public double B
        {
            get => b;
            set { if (SetProperty(ref b, value)) { } }
        }

        private double c = 4;
        public double C
        {
            get => c;
            set { if (SetProperty(ref c, value)) { } }
        }

        #region 柱状图2


        private SeriesCollection barChartSeries = new();

        public SeriesCollection BarChartSeries
        {
            get => barChartSeries;
            set => SetProperty(ref barChartSeries, value);
        }

        private ObservableCollection<string> barChartLabels;

        /// <summary>
        /// X轴标签
        /// </summary>
        public ObservableCollection<string> BarChartLabels
        {
            get => barChartLabels;
            set => SetProperty(ref barChartLabels, value);
        }

        private void InitializeBarData()
        {
            // 初始化柱状图数据
            var initialValues = new List<double> { A, B, C };
            var initialColors = new List<Brush> { Brushes.Purple, Brushes.Red, Brushes.Blue };

            for (int i = 0; i < initialValues.Count; i++)
            {
                var series = new ColumnSeries
                {
                    Title = $"Bar {i + 1}",
                    Values = new ChartValues<double>() { initialValues[i] },
                    Fill = initialColors[i]
                };
                BarChartSeries.Add(series);
            }
        }

        private void UpdateBarData()
        {
            var series0 = BarChartSeries[0] as ColumnSeries;
            var series1 = BarChartSeries[1] as ColumnSeries;
            var series2 = BarChartSeries[2] as ColumnSeries;
            series0.Values[0] = A;
            series1.Values[0] = B;
            series2.Values[0] = C;
            //var random = new Random();
            //for (int i = 0; i < BarChartSeries.Count; i++)
            //{
            //    var series = BarChartSeries[i] as ColumnSeries;
            //    if (series != null)
            //    {
            //        series.Values[0] = (double)random.Next(1, 100); // 将整数转换为双精度浮点数
            //    }
            //}
        }

        #endregion

        #region 饼图
        private void InitializePieData()
        {
            // 初始化饼图数据
            var initialValues = new List<double> { A, B, C };
            var initialColors = new List<Brush> { Brushes.Purple, Brushes.Red, Brushes.Blue };

            for (int i = 0; i < initialValues.Count; i++)
            {
                var series = new PieSeries
                {
                    Title = $"Pie {i + 1}",
                    Values = new ChartValues<double> { initialValues[i] },
                    Fill = initialColors[i]
                };
                PieValues.Add(series);
            }
        }

        private void UpdatePieData()
        {
            var series0 = PieValues[0] as PieSeries;
            var series1 = PieValues[1] as PieSeries;
            var series2 = PieValues[2] as PieSeries;
            series0.Values[0] = A;
            series1.Values[0] = B;
            series2.Values[0] = C;
        }

        //private void UpdatePieData()
        //{
        //    var random = new Random();

        //    for (int i = 0; i < PieValues.Count; i++)
        //    {
        //        var series = PieValues[i] as PieSeries;
        //        if (series != null)
        //        {
        //            series.Values[0] = (double)random.Next(1, 100); // 将整数转换为双精度浮点数
        //        }
        //    }
        //}

        private SeriesCollection pieValues;

        public SeriesCollection PieValues
        {
            get => pieValues;
            set => SetProperty(ref pieValues, value);
        }

        #endregion

        #region 折线图

        private SeriesCollection lineSeries = new();

        public SeriesCollection LineSeries
        {
            get => lineSeries;
            set => SetProperty(ref lineSeries, value);
        }

        private void InitializeLineData()
        {
            // 初始化折线图数据
            var initialValues = new List<double> { A, B, C };
            var initialColors = new List<Brush> { Brushes.Orange, Brushes.Purple, Brushes.Yellow };

            for (int i = 0; i < 3; i++)
            {
                var series = new LineSeries
                {
                    Title = $"Line {i + 1}",
                    Values = new ChartValues<double>(),
                    Stroke = initialColors[i],
                    Fill = null
                };
                LineSeries.Add(series);
            }
        }

        private void UpdateLineData()
        {
            var series0 = LineSeries[0] as LineSeries;
            var series1 = LineSeries[1] as LineSeries;
            var series2 = LineSeries[2] as LineSeries;

            series0.Values.Add(A);
            series1.Values.Add(B);
            series2.Values.Add(C);
            XAxisLabels.Add(DateTime.Now.ToString("HH:mm:ss"));

            if (series0.Values.Count > 10)
            {
                series0.Values.RemoveAt(0);
            }
            if (series1.Values.Count > 10)
            {
                series1.Values.RemoveAt(0);
            }
            if (series2.Values.Count > 10)
            {
                series2.Values.RemoveAt(0);
            }

            if (XAxisLabels.Count > 10)
            {
                XAxisLabels.RemoveAt(0);
            }
        }

        private ChartValues<double> lineChartValuesA = new();

        public ChartValues<double> LineChartValuesA
        {
            get => lineChartValuesA;
            set => SetProperty(ref lineChartValuesA, value);
        }

        private ChartValues<double> lineChartValuesB = new();
        public ChartValues<double> LineChartValuesB
        {
            get => lineChartValuesB;
            set => SetProperty(ref lineChartValuesB, value);
        }

        private ChartValues<double> lineChartValuesC = new();
        public ChartValues<double> LineChartValuesC
        {
            get => lineChartValuesC;
            set => SetProperty(ref lineChartValuesC, value);
        }

        private ObservableCollection<string> xAxisLabels = new();
        public ObservableCollection<string> XAxisLabels
        {
            get => xAxisLabels;
            set => SetProperty(ref xAxisLabels, value);
        }

        #endregion
    }
}
